package cmd

import (
	"fmt"
	"os"
	"strings"

	"gitee.com/workits/pkgs/toolx"
	"gitee.com/workits/pkgs/utilx"
	"github.com/fzdwx/infinite/components"
	"github.com/spf13/cobra"
)

var installCmd = &cobra.Command{
	Use:   "install",
	Short: "安装workits客户端",
	Long:  "安装workits客户端",
	Run: func(cmd *cobra.Command, args []string) {
		// 获取用户临时目录
		userHome, err := os.UserHomeDir()
		if err != nil {
			panic("🙅获取用户临时目录失败")
		}

		// 判断是否已存在安装
		if utilx.FsIsExist(userHome + toolx.PS + ".workits") {
			existInput := components.NewInput()
			existInput.Prompt = "workits客户端已经存在, 是否要将模版更至新最新版本(y/n)? "
			_ = components.NewStartUp(existInput).Start()
			if strings.ToLower(existInput.Value()) != "y" {
				os.Exit(0)
			}
		}

		toolx.DownloadTemplates(true)
		fmt.Println("🎉workits客户端初始化完成")
	},
}

func init() {
	rootCmd.AddCommand(installCmd)
}
